package com.example.hotel_demo.controller;


import com.example.hotel_demo.service.RegistService;
import com.example.hotel_demo.utils.MailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;


@Controller
@ComponentScan
public class RegistController {
    @Autowired
    private RegistService registService;

    @Autowired
    private MailUtil mailUtil ;

    @Transactional
    @PostMapping("/registCheck")
    @ResponseBody
    public String regist( String username,  String password,String repassword,  String email,String codeInput, HttpSession session) {
        String codeEmail = (String) session.getAttribute("codeEmail");
        String tt= registService.check( username,   password, repassword,   email, codeInput,codeEmail);
        return tt;
    }

    @RequestMapping("/register")
    public String registYeMian(){
        return "register";
    }

    @RequestMapping("/getCodeEmail")
    @ResponseBody
    public int getCheckCode(@RequestBody String email, HttpSession session){
       //String email = codeAjax.getEmail();
         //  1094238102@qq.com
        String codeEmail = mailUtil.getCheckCode(email);
        System.out.println("发送到："+email+"的验证码为："+codeEmail);
        session.setAttribute("codeEmail",codeEmail);
        session.setAttribute("email",email);
        int flag = 200;
        return flag ;
    }

}
